package sample.controller;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import sample.util.Calculation;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @author czy
 * @create 2021-08-31 9:10
 * 计算器的控制器
 */
public class ApplicationController implements Initializable {

    /**
     * 标识最后一个字符是否为计算符号,包括/ * + - ( )
     */
    boolean endCharisSymbol;

    Calculation calculate = new Calculation();

    @FXML
    private Button nine;

    @FXML
    private Button six;

    @FXML
    private Button one;

    @FXML
    private Button subtract;

    @FXML
    private TextArea show;

    @FXML
    private TextArea result;

    @FXML
    private Button seven;

    @FXML
    private Button right;

    @FXML
    private TextArea suffix;

    @FXML
    private Button two;

    @FXML
    private Button three;

    @FXML
    private Button clear_action;

    @FXML
    private Button remaining;

    @FXML
    private Button eight;

    @FXML
    private Button zero;

    @FXML
    private Button backspace;

    @FXML
    private Button left;

    @FXML
    private Button four;

    @FXML
    private Button and;

    @FXML
    private Button divide;

    @FXML
    private Button multiply;

    @FXML
    private Button five;

    @FXML
    private Button equality;

    @FXML
    void one_action(ActionEvent event) {
        show.appendText(((Button) event.getSource()).getText());
    }

    @FXML
    void two_action(ActionEvent event) {
        show.appendText(((Button) event.getSource()).getText());
    }

    @FXML
    void three_action(ActionEvent event) {
        show.appendText(((Button) event.getSource()).getText());
    }

    @FXML
    void backspace_action(ActionEvent event) {
        try {
            show.setText(show.getText().substring(0, show.getText().length() - 1));
        } catch (Exception e) {
        }
        //如果显示的文本的为空,将结果和后缀表达式也同时设置为空
        if (show.getText().length() == 0) {
            result.clear();
            suffix.clear();
        }
    }

    @FXML
    void four_action(ActionEvent event) {
        show.appendText(((Button) event.getSource()).getText());
    }

    @FXML
    void five_action(ActionEvent event) {
        show.appendText(((Button) event.getSource()).getText());
    }

    @FXML
    void six_action(ActionEvent event) {
        show.appendText(((Button) event.getSource()).getText());
    }

    @FXML
    void divide_action(ActionEvent event) {
        show.appendText(((Button) event.getSource()).getText());
    }

    @FXML
    void seven_action(ActionEvent event) {
        show.appendText(((Button) event.getSource()).getText());
    }

    @FXML
    void eight_action(ActionEvent event) {
        show.appendText(((Button) event.getSource()).getText());
    }

    @FXML
    void nine_action(ActionEvent event) {
        show.appendText(((Button) event.getSource()).getText());
    }

    @FXML
    void multiply_action(ActionEvent event) {
        show.appendText(((Button) event.getSource()).getText());
    }

    @FXML
    void left_action(ActionEvent event) {
        show.appendText(((Button) event.getSource()).getText());
    }

    @FXML
    void zero_action(ActionEvent event) {
        show.appendText(((Button) event.getSource()).getText());
    }

    @FXML
    void right_action(ActionEvent event) {
        show.appendText(((Button) event.getSource()).getText());
    }

    @FXML
    void subtract_action(ActionEvent event) {
        show.appendText(((Button) event.getSource()).getText());
    }

    @FXML
    void and_action(ActionEvent event) {
        show.appendText(((Button) event.getSource()).getText());
    }

    @FXML
    void clear_action(ActionEvent event) {
        //清空文本域中的内容
        show.clear();
        result.clear();
        suffix.clear();
    }

    @FXML
    void remaining_action(ActionEvent event) {
        //show.appendText(((Button) event.getSource()).getText());
    }

    @FXML
    void equality_action(ActionEvent event) {
        try {
            Object reduced = calculate.reduced(show.getText());
            result.setText(reduced.toString());
            List<String> list = calculate.list;
            StringBuffer sb = new StringBuffer();
            for (String s : list) {
                sb.append(s + " ");
            }
            suffix.setText(sb.toString());
        } catch (Exception e) {
            Alert alert = new Alert(AlertType.WARNING);
            alert.setContentText("您输入的计算式有误 , 请重试");
            alert.show();
        }
    }

    private List op = new ArrayList();

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        op.add("*");
        op.add("/");
        op.add("+");
        op.add("-");
        op.add("(");
        op.add(")");
        //监听文本域中值的变化
        show.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                //获取倒数第二个字符
                String x = null;
                //获取倒数第一个字符
                String y = null;
                try {
                    x = show.getText().substring(show.getText().length() - 2, show.getText().length() - 1);
                    y = show.getText().substring(show.getText().length() - 1);
                } catch (Exception e) {

                }
                //如果倒数第二字符和倒数第一字符均是计算符号,则将倒数第二字符删除,达到替换的效果
                if (op.contains(y)) {
                    if (op.contains(x)) {
                        show.setText(show.getText().substring(0, show.getText().length() - 2) + show.getText().substring(show.getText().length() - 1));
                    }
                }
            }
        });

    }
}
